package com.jifeng.br.pjj.bookreader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.br.pjj.bookreader.entity.BookLikeEntity;
import com.jifeng.br.pjj.bookreader.mapper.BookLikeMapper;
import com.jifeng.br.pjj.bookreader.service.BookLikeService;
import com.jifeng.br.pjj.bookreader.util.R;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 图书收藏表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@Service
public class BookLikeServiceImpl extends ServiceImpl<BookLikeMapper, BookLikeEntity> implements BookLikeService {

    @Override
    public R checkLike(BookLikeEntity like) {
        like = this.getOne(new QueryWrapper<BookLikeEntity>().eq("book_id", like.getBookId()).eq("user_id", like.getUserId()));
        return R.data(like != null);
    }

    @Override
    public void saveOrCancel(BookLikeEntity like) {
        BookLikeEntity bookLike = this.getOne(new QueryWrapper<BookLikeEntity>().eq("book_id", like.getBookId()).eq("user_id", like.getUserId()));
        if (bookLike == null) {
            like.setLikeTime(new Date());
            this.save(like);
        } else {
            this.removeById(bookLike.getId());
        }
    }
}
